package com.example.demo.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.demo.entity.SysUser;
import com.example.demo.service.SysUserService;
import com.example.demo.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/sys_user")
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;

    @RequestMapping("/list")
    public List<SysUser> list() {
        List<SysUser> list = sysUserService.list();
        return list;
    }

    @GetMapping("/page")
    public Result page(@RequestParam Integer pageNum, @RequestParam Integer pageSize, SysUser params) {
        IPage<SysUser> page = sysUserService.getByPage(pageNum, pageSize, params);
        return Result.ok(page);
    }

    @GetMapping("/{userId}")
    public Result get(@PathVariable Long userId) {
        SysUser sysUser = sysUserService.getById(userId);
        sysUser.setPassword(null);
        return Result.ok(sysUser);
    }

    @PostMapping
    public Result save(@RequestBody SysUser sysUser) {
        sysUser.setCreateTime(LocalDateTime.now());
        sysUserService.save(sysUser);
        return Result.ok();
    }

    @PutMapping
    public Result update(@RequestBody SysUser sysUser) {
        sysUser.setUpdateTime(LocalDateTime.now());
        sysUserService.updateById(sysUser);
        return Result.ok();
    }

    @PutMapping("/reset_pwd")
    public Result resetPwd(@RequestBody SysUser sysUser) {
        sysUser.setUpdateTime(LocalDateTime.now());
        sysUserService.updateById(sysUser);
        return Result.ok();
    }

    @DeleteMapping("/{userId}")
    public Result delete(@PathVariable Long userId) {
        sysUserService.removeById(userId);
        return Result.ok();
    }


}